(function($, im, doc) {
	var Video2dataURL = function(path) {
		/*
		im.sendMsg({
			sender: 'self',
			type: 'video',
			content: path
		});*/
		im.sendMsg({
			type: 'video',
			content: path
		}, 1);
		im.closeFooterBox();
		plus.io.resolveLocalFileSystemURL(path, function(entry) {
			entry.file(function(file) {

				var reader = new plus.io.FileReader();
				reader.onloadend = function(e) {
					console.log(e.target.result);
					//var base64 = e.target.result.replace('data:video/mp4;base64,', '');
					var base64 = e.target.result;
					//console.log(base64);
					$.post(upurl, {//gz-修改
						file: base64,
						type: 'video',
						uid : im.uid
					}, function(res) {
						console.log(res.status);
						if(res.status > 0) {
							im.sendMsg({
								type: 'video',
								content: serverurl + res.url
							}, 2);
							console.log(res.url);
						} else {
							$.toast('发送失败！');
						}
					});
				};
				reader.readAsDataURL(file);
			}, function(e) {
				mui.toast("读写出现异常: " + e.message);
			})
		})
	};
	function video_android(){
		var File = plus.android.importClass("java.io.File");
		var Uri = plus.android.importClass("android.net.Uri");
		var MediaStore = plus.android.importClass("android.provider.MediaStore");
		var Intent = plus.android.importClass("android.content.Intent");
		var intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		//intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); //0 最低质量, 1高质量MediaStore.EXTRA_SIZE_LIMIT
		//intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
		//intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10); //控制录制时间单位秒
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
		//限制时长
		intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 8);

		var main = plus.android.runtimeMainActivity();
		main.startActivityForResult(intent, 200);
		// 调用原生android摄像头
//      var VIDEOZOOM = 200;
//      var MediaStore = plus.android.importClass("android.provider.MediaStore");
//      var Intent = plus.android.importClass("android.content.Intent");
//      // 导入后可以使用new方法创建类的示例对象
//      var intent = new Intent("android.media.action.VIDEO_CAPTURE");
//      intent.putExtra("android.intent.extra.videoQuality", 0.7);//0 means low quality, 1 means high quality
//      //intent.putExtra("android.provider.MediaStore.EXTRA_OUTPUT", url);
//      intent.putExtra("android.intent.extra.durationLimit", 10);//设置录像时间
//
//      var main = plus.android.runtimeMainActivity();
//      main.startActivityForResult(intent,VIDEOZOOM);
        //获取返回参数
        
        main.onActivityResult = function(requestCode, resultCode, data) {
            var context = main;
            plus.android.importClass(data);
            var uri = data.getData();
            var resolver = context.getContentResolver();
            plus.android.importClass(resolver);
            var cursor = resolver.query(uri, null, null, null, null);
            plus.android.importClass(cursor);
            cursor.moveToFirst();
            var column = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            // 获取录制的视频路径
            var filePath = cursor.getString(column);
			//var path = plus.io.convertLocalFileSystemURL(filePath);
			//console.log(path);
			Video2dataURL(filePath); 
            // 解析视频文件的属性
            /*
            plus.io.resolveLocalFileSystemURL(filePath,function(entry) {
                entry.file(function(file){
                		 console.log(JSON.stringify(file));
                    console.log("size=="+file.size); 
                    console.log("name=="+file.name);
                });
            }, function (e) {
                console.log("Resolve file URL failed: " + e.message);
            });*/
        };
	}
	function video_ios(){
		var cmr = plus.camera.getCamera();
		//获取相机
		cmr.startVideoCapture(function(p) {
			//录像
			plus.io.resolveLocalFileSystemURL(p, function(entry) {
				//通过URL参数获取目录对象或文件对象 就是获取到录像文件位置    
				console.log(p); //打印文件位置，可以直接用作上传路径
				var path = plus.io.convertLocalFileSystemURL(p);
				console.log(path);
				Video2dataURL(path);
			}, function(e) {
				console.log("读取录像文件错误：" + e.message);
			});

		}, function(e) {
			console.log("失败");
			plus.nativeUI.toast("录像失败！", {
				duration: "long"
			});
		}, {
			filename: "_doc/camera/", //文件保存目录
			index: 2
		});
	}
	// 录像
	function getVideo() {
		//console.log($.os.android);
		if($.os.android) {
			video_android();
		}else{
			video_ios();
		}
		
		/*
		var File = plus.android.importClass("java.io.File");
		var Uri = plus.android.importClass("android.net.Uri");
		var MediaStore = plus.android.importClass("android.provider.MediaStore");
		var Intent = plus.android.importClass("android.content.Intent");
		var intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); //0 最低质量, 1高质量MediaStore.EXTRA_SIZE_LIMIT
		//intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
		intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10); //控制录制时间单位秒
		var main = plus.android.runtimeMainActivity();
		main.startActivityForResult(intent, 1);
		main.onActivityResult = function(request, code, data) {
			console.log("code" + code + "request" + request);
			console.log(JSON.stringify(data));
			if(request == 1001) { //相册Activity返回码

			} else {
				plus.gallery.pick(function(path) {
					Video2dataURL(path);
				}, function(e) {
					console.log("取消选择视频");
				}, {
					filter: "video" //筛选类型
				});
			}
		}*/

	}
	// Android平台的视频全屏旋转
	var fullScreenOfAndroid = function() {
		if(true) {
			// 最新5+API的支持
			var self = plus.webview.currentWebview();
			self.setStyle({
				videoFullscreen: 'landscape'
			});
		} else {
			// 如果暂未更新sdk，可以先使用差量升级等方式，做出兼容处理；
			// 旧版本下的兼容处理
			document.addEventListener('webkitfullscreenchange', function() {
				var el = document.webkitFullscreenElement; //获取全屏元素
				if(el) {
					plus.screen.lockOrientation('landscape'); //锁死屏幕方向为横屏
				} else {
					plus.screen.unlockOrientation(); //解除屏幕方向的锁定
				}
			});

		}
	};
	// iOS平台的视频全屏旋转
	var fullScreenOfIos = function(videoElem) {
		// 监听的事件与Android平台有很大区别
		videoElem.addEventListener('webkitbeginfullscreen', function() {
			plus.screen.lockOrientation('landscape'); //锁死屏幕方向为横屏
		});
		videoElem.addEventListener('webkitendfullscreen', function() {
			plus.screen.unlockOrientation(); //解除屏幕方向的锁定
		});
	};

	function showVideo() {
		/*
		var path = this.getAttribute('data-path');
		var service_comfirm = plus.webview.getWebviewById("video_show.html");
		service_comfirm && plus.webview.close(service_comfirm, "none");
		mui.openWindow({
			url: "video_show.html",
			id: "video_show.html",
			extras : {
				src : path
			}
		});
		*/
		var path = this.getAttribute('data-path');
		path = app.file(path) || path;
		//console.log(path);
		var Intent = plus.android.importClass("android.content.Intent");
		var Uri = plus.android.importClass("android.net.Uri");
		var main = plus.android.runtimeMainActivity();
		var intent = new Intent(Intent.ACTION_VIEW);
		var uri = Uri.parse(path);
		intent.setDataAndType(uri, "video/mp4");
		main.startActivity(intent);
	}
	$.plusReady(function() {
		var takevideo = document.getElementById("takeVideo");
		takevideo && takevideo.addEventListener('tap', getVideo);
		$('#msg-list').on('tap', '.msg-videobox', showVideo);
	});

}(mui, im, document));